﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using xClient.Framework;
using xClient.Framework.BaseView;
using xClient.Framework.PlugCore;

namespace cMES4004
{
    [Plug(ID = "cMES4004",
          Name = "LOG查询",
          Description = "LOG查询",
          Parent = "3,数据查询")]
    public partial class cMES4004 : StdView
    {
        public cMES4004()
        {
            InitializeComponent();
        }
        private void cMES1000_Load(object sender, EventArgs e)
        {
            LoadGroupList();
        }

        private void LoadGroupList()
        {
            cbGroupName.Items.Clear();
            ExecutionResult<dynamic> res = Services.ServiceMethod.GetGroupList();
            if (!res.Status)
            {
                ShowMessage(res.Message, MsgType.NG);
            }
            else
            {
                if (res.Data.Count > 0)
                {
                    JArray groupList = res.Data;

                    if (groupList.Count>0)
                    {
                        for (int i = 0; i < groupList.Count; i++)
                        {
                            if (!cbGroupName.Items.Contains(groupList[i]["group_name"].ToString()))
                            {
                                cbGroupName.Items.Add(groupList[i]["group_name"].ToString());
                            }
                        }
                    }
                    
                    ShowMessage("Group查询成功", MsgType.OK);
                }
                else
                {
                    ShowMessage("Group没有查询到信息", MsgType.NG);
                }
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            string sn = string.Empty, groupName = string.Empty;
            DateTime dtstart = new DateTime();
            DateTime dtend = new DateTime();
            if (!string.IsNullOrEmpty(txtSerialnumber.Text))
            {
                sn = txtSerialnumber.Text;
            }
            if (!string.IsNullOrEmpty(cbGroupName.Text))
            {
                groupName = cbGroupName.Text;
                if (string.IsNullOrEmpty(sn))
                {
                    ShowMessage("选择站别时请填写好SN信息", MsgType.NG);
                    return;
                }
            }
            dtstart = dateTimeStart.Value.Date;
            dtend = dateTimeEnd.Value.Date;

            if (dtend < dtstart)
            {
                ShowMessage("时间选择结束时间需要大于开始时间", MsgType.NG);
                return;
            }

            string vm = JsonConvert.SerializeObject(new
            {
                SN = sn,
                GROUPNAME = groupName,
                DTS = dtstart,
                DTE= dtend
            });

            ExecutionResult<dynamic> res = Services.ServiceMethod.GetWipLogInfo(vm);

            if (!res.Status)
            {
                ShowMessage(res.Message, MsgType.NG);
            }
            else
            {
                if (res.Data.Count > 0)
                {
                    datalist.DataSource = res.Data;
                    ShowMessage("查询成功", MsgType.OK);
                }
                else
                {
                    datalist.Rows.Clear();
                    ShowMessage($"没有查到", MsgType.NG);
                }

                datalist.ClearSelection();
            }
        }

    }
}
